package entity.lemming;

import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;

import entity.Entity;
import entity.terrain.Terrain;
import level.Level;

/**
 * A lemming that digs diagonal (downwards) through the terrain.
 * 
 * @author Neil Cowzer
 */
public class Miner extends Lemming {
	
	private int cycle = 0;

	/**
	 * Create an instance of the Miner Lemming.
	 * 
	 * @param x the starting x-coordinate.
	 * @param y the starting y-coordinate.
	 * @param level the current level.
	 */
	/*@ requires 0 <= x && 0 <= y;
		assignable speed_X && speed_Y && energy;
		ensures speed_X != 0 && speed_Y < 0 && energy == 100;
	 */
	public Miner(int x, int y){
		super("resource/graphics/lemming/miner/miner_1.gif",15, x, y);
		speed_X = 1;
		speed_Y = 1;
		energy = 100;
	}
	
	/**
	 * Create a new Miner with the given center point
	 * 
	 * @param center point of the Lemming
	 */
	public Miner(Point center){
		super("resource/graphics/lemming/miner/miner_1.gif",15, center);
		speed_X = 1;
		speed_Y = 1;
		energy = 100;
	}
	
	/**
	 * Checks for a collision with another Entity
	 * 
	 * @param other another Entity eg Terrain, Lemming.
	 * @return true if a collision has occured.
	 */
	public /*@ pure @*/ boolean collision(Lemming other) {
		// TODO Auto-generated method stub
		return false;
	}

	/** Request that this lemming perform the appropiate action/movement. */
	/*@ ensures x == \old(x) + speed_X &&
	 * 			y == \old(y) + speed_Y &&
				energy == \old(energy) - 1;
	 */
	public void move(Terrain terrain) {
		boolean mine = false;
		Rectangle lemming = getBounds();
		
		if (lemming.intersects(terrain.getBounds())){
			cycle++;
			BufferedImage image = ((Terrain)terrain).getImage().getBufferedImage();
			int black = -16777216; // black that represents the background
			int black2 = -16776960;
			try{
				if (speed_X > 0){
					// if the is at least one non- black pixel - dig
					while (!mine){
							for (int i = getX()+lemming.width/2; i < getX() + lemming.width+2; i++){
								for (int k = getY()+lemming.height/3; k < getY()+lemming.height+2;k++){
									int color = image.getRGB(i, k);
			
									if (color != black){
										mine = true;
										break;
									}
								}
							}
						break;
					}


					if (mine){
						terrain.destroyTerrain(x+lemming.width, y, "miner");
					} else {
						energy = 1;
					}
				}else {
					// if the is at least one non- black pixel - dig
					while (!mine){
							for (int i = getX()-2; i < getX() + lemming.width/2; i++){
								for (int k = getY()+lemming.height/3; k < getY()+lemming.height+2;k++){
									int color = image.getRGB(i, k);
			
									if (color != black){
										mine = true;
										break;
									}
								}
							}
						break;
					}


					if (mine){
						terrain.destroyTerrain(x, y, "miner");
					} else {
						energy = 1;
					}
				}
			}catch(ArrayIndexOutOfBoundsException e){}
		}
		
				//update positions
		if (cycle%3 == 0){
			x= x+speed_X;
			y= y+speed_Y;
		}
		update();
		energy--;	}

	/** Reverse a Lemmings direction */
	public void turnAround(){
		speed_X = -(speed_X);
		if (speed_X > 0){
			generateFrames("resource/graphics/lemming/miner/miner_1.gif", 15);
		} else{
			generateFrames("resource/graphics/lemming/miner/Leftminer_1.gif", 15);
		}
	}
}
